import libtcodpy as libtcod
import map
import utils
import interfaces
from keyboard import handle_keyboard
from objects import read_monsters, read_items

#===============================================================================
# GAME CLASS
#===============================================================================

PLAYER_DEFAULTS = {
                   'name': 'Player',
                   'level': 1,
                   'hp': 10,
                   'max_hp': utils.roll_dice(3, 6, 10),
                   'regen_rate': 2,
                   'xp': 9,
                   'attack': 4,
                   'defense': 0,
                   'next_lvl': 10,
                   'inventory': [],
                   
                   'equipment': {
                                 'weapon': None,
                                 'shield': None,
                                 'rings': {
                                           'left': None,
                                           'right': None
                                           }
                                 }
                   }

class Game(object):
    #Init
    def __init__(self):
        self.stop = False
        self.player_data = PLAYER_DEFAULTS
        
        #Keyboard
        self.wait_for_keyboard = True
        self.key = None
        
        self.monster_list = read_monsters(utils.FILES['monsters'])
        self.item_list = read_items(utils.FILES['items'])
        
        #Interfaces
        self.hud = interfaces.Hud(self)
        self.hud.add_message('Welcome, for help press <? or F1>; <SPACE> to continue', ' ')
        self.help = interfaces.Help(self)
        self.inventory = interfaces.Inventory(self)
        self.death_screen = interfaces.Death()
        self.death = False
        
        #Map
        self.current_depth = 0
        self.current_map = map.Dungeon(self, level = self.current_depth)
        self.maps = []
        self.maps.append(self.current_map)
        
    def prev_map(self):
        if self.current_depth - 1 < 0:
            self.hud.add_message('The stairs are blocked!')
            return False
        else:
            self.current_depth -= 1
            self.current_map = self.maps[self.current_depth]
            return True
    
    def next_map(self):
        if self.current_depth + 1 >= len(self.maps):
            self.current_depth += 1
            self.current_map = map.Dungeon(self, level = self.current_depth)
            self.maps.append(self.current_map)
        else:
            self.current_depth += 1
            self.current_map = self.maps[self.current_depth]
    
    #Draw
    def draw(self):
        if self.help.show:
            self.help.draw()
        else:
            self.current_map.draw()
            self.hud.draw()
            self.inventory.draw()
    
    #Update
    def update(self):
        if self.wait_for_keyboard:
            self.key = libtcod.console_wait_for_keypress(True)
        else:
            self.key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED)
        handle_keyboard(self, self.key)
            
        #Update Map
        self.current_map.update(self.key)
    
    #Loop
    def loop(self):
        while not self.stop:
            libtcod.console_clear(0)
            self.draw()
            libtcod.console_flush()
            self.update()
            
            if libtcod.console_is_window_closed():
                self.stop = True
                
    #End
    def end(self):
        if self.death:
            self.death_screen.draw()

if __name__ == "__main__":
    print "Please run main.py!"
